package com.itheima.mall.api.ums;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.mapper.ums.UmsMenuMapper;
import com.itheima.mall.pojo.ums.UmsMenu;
import com.itheima.mall.vo.PageResult;
import com.itheima.mall.vo.R;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@DubboService
public class UmsMenuServiceImpl implements UmsMenuServiceApi {

    @Autowired
    UmsMenuMapper umsMenuMapper;

    @Override
    public R UmsMenuCreate(UmsMenu umsMenu) {
        umsMenu.setCreateTime(LocalDateTime.now());
        int flag = umsMenuMapper.insert(umsMenu);
        if (flag>0){
            return R.succeed("","创建成功");
        }
        return R.error("创建失败");
    }

    @Override
    public R UmsMenuDelete(Long id) {
        int flag = umsMenuMapper.deleteById(id);
        if (flag>0){
            return R.succeed("","删除成功");
        }
        return R.error("删除失败");    }

    @Override
    public R UmsMenuList(Long parentId, Integer pageNum, Integer pageSize) {

        Page pageCondition = new Page(Convert.toLong(pageNum),Convert.toLong(pageSize));
        LambdaQueryWrapper<UmsMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ObjectUtil.isNotNull(parentId),UmsMenu::getParentId,parentId);
        lqw.eq(UmsMenu::getHidden,0);

        Page pageR = umsMenuMapper.selectPage(pageCondition, lqw);
        PageResult pageResult = new PageResult();
        pageResult.setList(pageR.getRecords());
        pageResult.setPageSize(pageSize);
        pageResult.setPageNum(pageNum);
        pageResult.setTotalPage(Convert.toInt(pageR.getPages()));
        pageResult.setTotal(Convert.toInt(pageR.getTotal()));

        return R.succeed(pageResult,"查询成功");
    }

    @Override
    public List UmsMenuTreeList() {

        LambdaQueryWrapper<UmsMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UmsMenu::getHidden,0);

        List<UmsMenu> umsMenus = umsMenuMapper.selectList(lqw);

        return umsMenus;
    }

    @Override
    public R UmsMenuUpdate(Long id, UmsMenu umsMenu) {
        umsMenu.setId(id);
        int flag = umsMenuMapper.updateById(umsMenu);
        if (flag>0){
            return R.succeed("","更新成功");
        }
        return R.error("更新失败");
    }

    @Override
    public R UmsMenuUpdateHidden(Long id, Integer hidden) {
        UmsMenu umsMenu = new UmsMenu();
        umsMenu.setId(id);
        umsMenu.setHidden(hidden);
        int flag = umsMenuMapper.updateById(umsMenu);
        if (flag>0){
            return R.succeed("","更新成功");
        }
        return R.error("更新失败");
    }

    @Override
    public R UmsMenuFindById(Long id) {
        UmsMenu umsMenu = umsMenuMapper.selectById(id);
        return R.succeed(umsMenu,"查询成功");
    }

    @Override
    public List UmsMenuListAll() {
        LambdaQueryWrapper<UmsMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UmsMenu::getHidden,0);
        return umsMenuMapper.selectList(lqw);
    }
}
